package joystickjava;

import PackagePoseidon.DirectionData;
import PackagePoseidon.DirectionDataPublisher;
import ops.Participant;
import ops.Topic;


/**
 * Francesco Lilli 01/08/2010
 *
 * The class OPSInterface is needed to make the joystick (project JoystickJava)
 * and the simulator (project Poseidon_Inter) communicate each other.
 * This class contains all the needed methods to send the information about the
 * direction into OPS.
 * 
 */
public class OPSInterface {

    //What is needed from OPS
    Topic topic;
    DirectionDataPublisher pub;
    Participant participant;
    DirectionData data;


    /**
     * Francesco Lilli 01/08/2010
     *
     * The method initializes all the needed parameters for OPS. It specifies:
     * - the source where to get access to the Participants;
     * - the Topics, what arguments we're going to talk about. In a Topic we can
     *   take into account several IDL data types;
     * - the Participants, who's going to use OPS in order to send/receive
     *   the needed information;
     * - the Data, the kinds of structure we're going to use.
     *
     * @param
     * @return
     */
    public void initOPS() {

        participant = Participant.getInstance("PoseidonDomain");
        participant.addTypeSupport(new OPSIDLPoseidonFinal.OPSIDLPoseidonFinalTypeFactory());

        topic = participant.createTopic("DirectionTopic");

        pub = new DirectionDataPublisher(topic);
        data = new DirectionData();
        pub.setName("Publisher");
    }

    
    /**
     * Francesco Lilli 01/08/2010
     *
     * The method writes into OPS the information about the direction.
     * When the device (joystick) is moved by the user, the method writes
     * different information depending on the direction.
     *
     * @param direction - the direction taken from the joystick. It is a string
     *                    which reperesents an integer number from 0 to 4 (to
     *                    represent real directions - forward, up, down, left,
     *                    right) or 10 (to represent a null direction).
     * @return
     */
    public void writeInfoOPS(int direction) {

        DirectionData dataDir = new DirectionData();

        switch (direction) {
            case 0: //the direction is FORWARD
                    dataDir.direction = "0";
                    pub.write(dataDir);
                    break;
            case 1: //the direction is UP
                    dataDir.direction = "1";
                    pub.write(dataDir);
                    break;
            case 2: //the direction is DOWN
                    dataDir.direction = "2";
                    pub.write(dataDir);
                    break;
            case 3: //the direction is LEFT
                    dataDir.direction = "3";
                    pub.write(dataDir);
                    break;
            case 4: //the direction is RIGHT
                    dataDir.direction = "4";
                    pub.write(dataDir);
                    break;
            case 10: //null direction (no joystick movement)
                    dataDir.direction = "10";
                    pub.write(dataDir);
                    break;
        }       
    }
}
